package com.aisino.api.model;

import com.aisino.api.model.ResultCode;
import lombok.Data;

@Data
public class SubStoreInfoRequest {
    private String appId;
    private String timestamp;
    private String reqId;
    private String uscc;
    private String sfyb;
    private String yysj;
    private String lxdh;
    private String longitude;
    private String latitude;
    private String img;
    private String sign;

    /**
     * 验证请求参数是否合法
     *
     * @return 验证结果，null表示验证通过，否则返回错误信息
     */
    public ValidationResult validate() {
        if (appId == null || appId.trim().isEmpty()) {
            return new ValidationResult(ResultCode.PARAM_ERROR.getCode(), "appId不能为空");
        }

        if (timestamp == null || timestamp.trim().isEmpty()) {
            return new ValidationResult(ResultCode.PARAM_ERROR.getCode(), "timestamp不能为空");
        }

        if (reqId == null || reqId.trim().isEmpty()) {
            return new ValidationResult(ResultCode.PARAM_ERROR.getCode(), "reqId不能为空");
        }

        if (uscc == null || uscc.trim().isEmpty()) {
            return new ValidationResult(ResultCode.PARAM_ERROR.getCode(), "uscc不能为空");
        }

        // 验证统一社会信用代码长度为18位
        if (uscc.length() != 18) {
            return new ValidationResult(ResultCode.PARAM_ERROR.getCode(), "uscc必须为18位统一社会信用代码");
        }

        if (sfyb == null || sfyb.trim().isEmpty()) {
            return new ValidationResult(ResultCode.PARAM_ERROR.getCode(), "sfyb不能为空");
        }

        // 验证是否医保定点药店字段值只能为1或2
        if (!"1".equals(sfyb) && !"2".equals(sfyb)) {
            return new ValidationResult(ResultCode.PARAM_ERROR.getCode(), "sfyb只能为1(是)或2(否)");
        }

        if (yysj == null || yysj.trim().isEmpty()) {
            return new ValidationResult(ResultCode.PARAM_ERROR.getCode(), "yysj不能为空");
        }

        // 验证营业时间格式，应为HH:mm~HH:mm格式
        if (!yysj.matches("\\d{2}:\\d{2}~\\d{2}:\\d{2}")) {
            return new ValidationResult(ResultCode.PARAM_ERROR.getCode(), "yysj格式不正确，应为HH:mm~HH:mm格式，例如08:00~23:00");
        }

        if (lxdh == null || lxdh.trim().isEmpty()) {
            return new ValidationResult(ResultCode.PARAM_ERROR.getCode(), "lxdh不能为空");
        }

        if (longitude == null || longitude.trim().isEmpty()) {
            return new ValidationResult(ResultCode.PARAM_ERROR.getCode(), "longitude不能为空");
        } else {
            try {
                double lon = Double.parseDouble(longitude.trim());
                if (lon < -180 || lon > 180) {
                    return new ValidationResult(ResultCode.PARAM_ERROR.getCode(), "longitude必须在-180到180之间");
                }
            } catch (NumberFormatException e) {
                return new ValidationResult(ResultCode.PARAM_ERROR.getCode(), "longitude格式不正确，必须为数字");
            }
        }

        if (latitude == null || latitude.trim().isEmpty()) {
            return new ValidationResult(ResultCode.PARAM_ERROR.getCode(), "latitude不能为空");
        } else {
            try {
                double lat = Double.parseDouble(latitude.trim());
                if (lat < -90 || lat > 90) {
                    return new ValidationResult(ResultCode.PARAM_ERROR.getCode(), "latitude必须在-90到90之间");
                }
            } catch (NumberFormatException e) {
                return new ValidationResult(ResultCode.PARAM_ERROR.getCode(), "latitude格式不正确，必须为数字");
            }
        }



        return null; // 验证通过
    }

    /**
     * 验证结果内部类
     */
    public static class ValidationResult {
        private int code;
        private String message;

        public ValidationResult(int code, String message) {
            this.code = code;
            this.message = message;
        }

        public int getCode() {
            return code;
        }

        public String getMessage() {
            return message;
        }
    }
}